import {Store, IOptions} from 'plume2';
import UserActor from './actor/user-actor';
import TemplateActor from './actor/template-actor';
import BackgroundActor from './actor/background-actor';
import EditorActor from './actor/editor-actor';
import ImageIndexActor from './actor/imageIndex-actor';
import releaseActor from './actor/release-actor';
import {fetchTemplateList, backgroundList, Release, publish, fetchDomainUrl} from './webapi';
import {loadJS} from '../../../util/index';
const antdJS = '//qianmi-web.oss-cn-hangzhou.aliyuncs.com/x-site/public/javascripts/antd.min.js';
const jQueryJS = 'http://qianmi-web.oss-cn-hangzhou.aliyuncs.com/x-site/prod/public/javascripts/jquery/jquery-2.2.4.min.js';
declare let window: any;

export default class AppStore extends Store {
    constructor(props: IOptions) {
        super(props);
        if (__DEV__) {
            (window as any)._store = this;
        }
    }

    bindActor() {
        return [new UserActor(), new TemplateActor(), new BackgroundActor(), new EditorActor(), new ImageIndexActor(), new releaseActor()];
    }

    //初始用户在pc发布后的页面URL
    getfetchDomainUrl = async () => {
        const res = await fetchDomainUrl();
        this.dispatch('tpl:getTplUrl', res);
    }
    //初始用户是否在pc上登陆编辑过
    getRelease = async () => {
        const res = await Release();
        this.dispatch('tpl:getRelease', res);
        this.dispatch('tpl:getTplRelease', res);
    }
    //用户pc编辑过，初次进入APP建站
    clickRelease = async (boolean) => {
        this.dispatch('tpl:getRelease', boolean);
    }
    //用户pc编辑过，初次在APP发布模板
    clickTplRelease = async (boolean) => {
        this.dispatch('tpl:getTplRelease', boolean);
    }
    //初始获取模板数据信息
    getTplList = async (SelectId?: string, content?: string) => {
        const res = await fetchTemplateList();
        console.log(res);
        this.dispatch('tpl:getList', res);
        this.dispatch('tpl:SelectId', SelectId);
        this.dispatch('tpl:SelectBannerImage', content);
        return res;
    };

    //获取背景默认列表数据
    getBjList = async () => {
        const res = await backgroundList();
        console.log(res);
        this.dispatch('tpl:getBjList', res);
        return res;
    };
    //获取Banner默认列表图索引值
    changeImageIndex = async bjIndex => {
        console.log(bjIndex);
        this.dispatch('tpl:getBjIndex', bjIndex);
        return bjIndex;
    };
    //获取Banner默认列表图或者上传的图路径，存入editor-actor中
    changeBannerImage = async url => {
        console.log(url);
        this.dispatch('tpl:bannerImage', {url});
        return url;
    };
    //单击模板获取模板的默认数据(url, id, boolean)，存入editor-actor中
    clickTplImage = async (url, id, boolean, _height, content, index) => {
        console.log(url);
        console.log(boolean);
        this.dispatch('tpl:tplImage', {url, id, activated: boolean, height: _height, content: content, index});
    };
    //进入模板选择页，清空editor-actor的url数据
    emptyData = async (url) => {
        this.dispatch('tpl:bannerImage', {url})
    }
    //启用模板，将用户选择数据传入OpenApi中
    useTemplate = async () => {
        // console.log(this.state())
        if (!window.antd) {
            await loadJS(antdJS);
        }
        if (!window.jQueryJS) {
            await loadJS(jQueryJS);
        }
        let tplCode = this.state().getIn(['editor', 'id']);
        let listBanner=this.state().getIn(['list'])[this.state().getIn(['editor', 'index'])].content;
        let url = this.state().getIn(['editor', 'bannerImage'])||this.state().getIn(['editor', 'content'])||listBanner;
        console.log(url);
        await publish({tplCode, url});
        this.dispatch('tpl:publish', {tplCode, url})
    }
}
